// Process.cpp

#include "StdAfx.h"

#include "Common/String/StringConvert.h"

#include "Process.h"

namespace Common {
namespace System {

static CString GetQuotedString(const CString &s)
{
  return CString(L'\"') + s + CString(L'\"');
}

DWORD CProcess::Create(LPCWSTR imageName, const CString &params, LPCWSTR curDir)
{
  CloseHandle();
  const CString params2 =
      #ifndef UNDER_CE
      GetQuotedString(imageName) + L' ' +
      #endif
      params;
  #ifdef UNDER_CE
  curDir = 0;
  #else
  imageName = 0;
  #endif
  PROCESS_INFORMATION pi;
  BOOL result;
  
  
	STARTUPINFO si;
	si.cb = sizeof(si);
	si.lpReserved = 0;
	si.lpDesktop = 0;
	si.lpTitle = 0;
	si.dwFlags = 0;
	si.cbReserved2 = 0;
	si.lpReserved2 = 0;
    
    result = CreateProcessW(imageName, (LPWSTR)(LPCWSTR)params2,
        NULL, NULL, FALSE, 0, NULL, (LPWSTR)curDir, &si, &pi);
  
  if (result == 0)
    return ::GetLastError();
  ::CloseHandle(pi.hThread);
  m_hHandle = pi.hProcess;
  return 0;
}

DWORD MyCreateProcess(LPCWSTR imageName, const CString &params)
{
  CProcess process;
  return process.Create(imageName, params, 0);
}

}
}